package com.net.xpay.core.manager.impl.auto;

import com.google.common.base.Preconditions;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.auto.AutoPayOrderMapper;
import com.net.xpay.core.domain.auto.AutoPayOrder;
import com.net.xpay.core.enums.AutoPayOrderStatus;
import com.net.xpay.core.manager.auto.AutoPayOrderManager;
import com.net.xpay.core.model.AutoPayOrderSearchModel;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Component
public class AutoPayOrderManagerImpl extends AbstractVersionedManager<AutoPayOrder> implements AutoPayOrderManager{

    @Autowired
    private AutoPayOrderMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(AutoPayOrder obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        return super.save(obj);
    }



    @Override
    protected BasicMapper<Long, AutoPayOrder> getMapper() {
        return mapper;
    }

    @Override
    public int batchSave(List<AutoPayOrder> records) {
        if (CollectionUtils.isEmpty(records)) {
            return 0;
        }
        for (AutoPayOrder record : records) {
            LocalDateTime now = LocalDateTime.now();
            record.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
            record.setCreateTime(now);
            record.setModifyTime(now);
            record.setVersion(0L);
        }
        return mapper.batchSave(records);
    }

    @Override
    public int batchUpdate(List<AutoPayOrder> records) {
        if (CollectionUtils.isEmpty(records)) {
            return 0;
        }
        for (AutoPayOrder record : records) {
            LocalDateTime now = LocalDateTime.now();
            record.setModifyTime(now);
            record.setVersion(record.getVersion() + 1L);
        }

        return mapper.batchUpdate(records);
    }

    @Override
    public List<AutoPayOrder> search(AutoPayOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(AutoPayOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public int updateAutoPayOrderStatus(Long userId, Long autoPayBatchId, AutoPayOrderStatus status) {
        Preconditions.checkArgument(userId != null && autoPayBatchId != null && status != null);
        return mapper.updateAutoPayOrderStatus(userId,autoPayBatchId,status);
    }

    @Override
    public List<AutoPayOrder> findByAutoPayTransBatchId(Long userId, Long autoPayTransBatchId) {
        Preconditions.checkArgument(userId != null && autoPayTransBatchId != null);
        AutoPayOrder condition = new AutoPayOrder();
        condition.setUserId(userId);
        condition.setAutoPayTransBatchId(autoPayTransBatchId);
        return listByCondition(condition, new ArrayList<>());
    }

    @Override
    public BigDecimal sumEnsuredAmount(Long userId) {
        Preconditions.checkArgument(userId != null);
        return mapper.sumEnsuredAmount(userId);
    }

    @Override
    public List<AutoPayOrder> list(AutoPayOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }
}
